#include <iostream>
#include <string>
#include <memory>

class Fruit{
public:
    Fruit(){}
    virtual void name() = 0;
};

class Apple : public Fruit{
public:
    Apple(){}
    void name() override
    {
        std::cout << "我是一个苹果" << std::endl;
    }    
};

class Banana : public Fruit{
public:
    Banana(){}
    void name() override
    {
        std::cout << "我是一个香蕉" << std::endl;
    }
};

class FruitFactory{
public:
    // 返回的是父类的指针，可以根据这个返回结果实现多态
    static std::shared_ptr<Fruit> create(const std::string& name)
    {
        if (name == "apple")
        {
            return std::make_shared<Apple>();
        }
        else if (name == "banana")
        {
            return std::make_shared<Banana>();
        }
        return nullptr;
    }
};

int main()
{
    std::shared_ptr<Fruit> fruit = FruitFactory::create("apple");
    fruit->name();
    fruit = FruitFactory::create("banana");
    fruit->name();

    return 0;
}
